import socket from './../plugins/websocket.js'

class vcsSocket {
    cws = null
    appKey = null
    limit = null
    url = null

    constructor(appKey, url, limit, callback) {
        if (this.cws) {
            return
        }
        this.appKey = appKey
        this.limit = limit
        this.url = url
        this.cws = new socket({
            url: url,
            reconnectTimes: null,
            reconnectTime: 60000,
            heartMsg: {method: 'ping', key: appKey},
            _init_func: (event) => {
                callback ? callback(event) : null
            }
        })
        window.vcsSocket = this
        return this.cws
    }

    dispose(callback) {
        if (this.cws) {
            this.cws.close()
            this.cws = null
        }
        window.vcsSocket = null
        callback ? callback(this.cws) : null
    }

    sendMessage(message){
        message.key = this.appKey
        if (this.cws) {
            this.cws.send(message)
        }else {
            console.error('not connect vcs socket!')
        }
    }

    /**
     * 加入会议室
     * @param roomName
     * @param roomNum
     * @param userName
     * @param type
     * @param address
     */
    joinConference(roomName, roomNum, userName, type, address) {
        let message = {
            method: 'joinRoom',
            memberType: type,
            roomName,
            userName,
            roomLimit: this.limit,
            roomNum,
            mediaAddress: address
        }
        this.sendMessage(message)
    }

    /**
     * 接收SDP
     * @param name
     * @param sdp
     */
    receive(name, sdp) {
        let message = {method: 'receiveVideoFrom', sender: name, sdpOffer: sdp}
        this.sendMessage(message)
    }

    /**
     * 离开会议室 只针对自己
     */
    leaveRoom() {
        let message = {method: 'leaveRoom'}
        this.sendMessage(message)
    }

    /**
     * 关闭会议室
     * @param roomName
     * @param roomNum
     */
    closeRoom(roomName,roomNum) {
        let message = {method: 'closeRoom', roomName, roomNum}
        this.sendMessage(message)
    }

    /**
     * 踢出会议室
     * @param roomName
     * @param roomNum
     * @param userName
     */
    kick(roomName, roomNum, userName) {
        let message = {method:'kickRoom',userName,roomName, roomNum}
        this.sendMessage(message)
    }

    /**
     * ICE 发送到服务器（与服务器交互ICE CANDIDATE）
     * @param candidate
     * @param userName
     */
    iceCandidate(candidate, userName) {
        let message = {method:'onIceCandidate',candidate, userName}
        this.sendMessage(message)
    }

    /**
     * 开始录像
     * @param roomName
     * @param roomNum
     */
    startRecord(roomName, roomNum) {
        let message = {method:'startRecord',roomName, roomNum}
        this.sendMessage(message)
    }

    /**
     * 结束录像
     * @param roomName
     * @param roomNum
     */
    stopRecord(roomName, roomNum) {
        let message = {method:'stopRecord',roomName, roomNum}
        this.sendMessage(message)
    }

    /**
     * 静音
     * @param roomName
     * @param roomNum
     * @param userName
     */
    mute(roomName, roomNum, userName) {
        let message = {method:'mute',roomName,roomNum, userName}
        this.sendMessage(message)
    }

    /**
     * 取消静音
     * @param roomName
     * @param roomNum
     * @param userName
     */
    unMute(roomName, roomNum, userName) {
        let message = {method:'unMute',roomName,roomNum, userName}
        this.sendMessage(message)
    }

    muteVideo() {
        let message = {method: 'muteVideo'}
        this.sendMessage(message)
    }

    unMuteVideo() {
        let message = {method: 'unMuteVideo'}
        this.sendMessage(message)
    }

    muteAll(roomName, roomNum) {
        let message = {method:'muteAll',roomName, roomNum}
        this.sendMessage(message)
    }

    unMuteAll(roomName, roomNum) {
        let message = {method:'unMuteAll',roomName, roomNum}
        this.sendMessage(message)
    }

    roomParticipantsNumber(roomName, roomNum) {
        let message = {method:'participantNumber',roomName, roomNum}
        this.sendMessage(message)
    }

    roomParticipants(roomName, roomNum) {
        let message = {method:'meetingParticipants',roomName, roomNum}
        this.sendMessage(message)
    }

    roomInfos() {
        let message = {method: 'roomInfo'}
        this.sendMessage(message)
    }

}

export default vcsSocket